package com.k2data.k2app.controller;

import com.k2data.k2app.domain.Region;
import com.k2data.k2app.mapper.RegionMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author lidong9144@163.com 17-6-23.
 */
@RestController
@RequestMapping("/regions")
public class RegionController {

    private final RegionMapper regionMapper;

    @Autowired
    public RegionController(RegionMapper regionMapper) {
        this.regionMapper = regionMapper;
    }

    @ApiOperation(value = "省")
    @GetMapping(value = "/province")
    public ResponseEntity<CommonResultResponse> getProvince() throws Exception {
        return ResponseHelper.success(removeDupR(regionMapper.selectProvinceCond()));
    }

    @ApiOperation(value = "市")
    @GetMapping(value = "/city")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "province", value = "province", dataType = "string", paramType = "query")
    })
    public ResponseEntity<CommonResultResponse> getCity(@RequestParam(value = "province", required = false) String province) throws Exception {
        Region cond = new Region();
        cond.setProvince(province);
        return ResponseHelper.success(removeDupC(regionMapper.selectCityCond(cond)));
    }

    @ApiOperation(value = "县")
    @GetMapping(value = "/district")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "province", value = "province", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "city", value = "city", dataType = "string", paramType = "query")
    })
    public ResponseEntity<CommonResultResponse> getDistrict(
            @RequestParam(value = "province", required = false) String province,
            @RequestParam(value = "city", required = false) String city
    ) throws Exception {
        Region cond = new Region();
        cond.setProvince(province);
        cond.setCity(city);

        return ResponseHelper.success(removeDupD(regionMapper.selectDistrictCond(cond)));
    }

    @ApiOperation(value = "获取")
    @GetMapping(value = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "province", value = "province", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "city", value = "city", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "district", value = "district", dataType = "string", paramType = "query")
    })
    public ResponseEntity<CommonResultResponse> getRegionCode(
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "province", required = false) String province,
            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "district", required = false) String district
    ) throws Exception {
        Region cond = new Region();
        cond.setCode(code);
        cond.setProvince(province);
        cond.setCity(city);
        cond.setDistrict(district);

        Region result = regionMapper.selectRegion(cond);

        return ResponseHelper.success(result == null ? new Region() : result);
    }

    private static ArrayList<Region> removeDupR(List<Region> users) {
        Set<Region> set = new TreeSet<Region>(new Comparator<Region>() {
            @Override
            public int compare(Region o1, Region o2) {
                return o1.getProvince().compareTo(o2.getProvince());
            }
        });
        set.addAll(users);
        return new ArrayList<Region>(set);
    }

    private static ArrayList<Region> removeDupC(List<Region> users) {
        Set<Region> set = new TreeSet<Region>(new Comparator<Region>() {
            @Override
            public int compare(Region o1, Region o2) {
                return o1.getCity().compareTo(o2.getCity());
            }
        });
        set.addAll(users);
        return new ArrayList<Region>(set);
    }

    private static ArrayList<Region> removeDupD(List<Region> users) {
        Set<Region> set = new TreeSet<Region>(new Comparator<Region>() {
            @Override
            public int compare(Region o1, Region o2) {
                //字符串,则按照asicc码升序排列
                return o1.getDistrict().compareTo(o2.getDistrict());
            }
        });
        set.addAll(users);
        return new ArrayList<Region>(set);
    }

}
